package com.fingard.dsp.bank.directbank.abc01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * 中国农业银行银企直联专线版(ABC01)单笔代付
 */
public class ABC01SinglePay extends ABC01Base implements ISinglePay {
	@Override
	public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1908);

			// 190801
		if ("190801".equals(p_req1908.reqHeader.commandCode)) {
			tmpRetDetail = d_190801(p_req1908);
			// 190804 | 190802&&同行&&对私
		} else if ("190804".equals(p_req1908.reqHeader.commandCode) || ("1".equals(p_req1908.singleDetail.isPrivate) && "1".equals(p_req1908.singleDetail.sameBnk))) {
			tmpRetDetail = d_190804(p_req1908);
			// 190802
		} else if ("190802".equals(p_req1908.reqHeader.commandCode)) {
			tmpRetDetail = d_190802(p_req1908);
		} else {
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = "不支持的交易指令:" + p_req1908.reqHeader.commandCode;
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	private RetAtsSglDetail d_190801(ReqSingle p_req1908) throws Exception {
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1908);

		try {
			ABC01Header tmpBankHeader = new ABC01Header();
			tmpBankHeader.transCode = ABC01TransCode.deal_1908_190801;
			tmpBankHeader.reqSeqID = p_req1908.singleDetail.askNumber;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHead(xmlWriter, tmpBankHeader);

			String trFlag = "";
			String tmpRootPay = p_req1908.singleDetail.rootPay;
			if (StringHelper.hasAnyChar(tmpRootPay)) {
				if ("0".equals(tmpRootPay)) {
					trFlag = "0";
				} else {
					trFlag = "1";
				}
			}
			String primaryAct = p_req1908.singleDetail.primaryAct;
			if (StringHelper.hasAnyChar(primaryAct)) {
				if ("0".equals(primaryAct) || primaryAct.equals(p_req1908.singleDetail.recAct)) {
					trFlag = "0";
				} else {
					trFlag = "1";
				}
			}

			XElement tmpXElement = new XElement("Corp", "");
			Date tmpDateNow = new Date();
			tmpXElement.addChild(new XElement("BookingDate", Format.Date8Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("BookingTime", Format.Time6Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("BookingFlag", "0"));
			if ("0".equals(trFlag)) { // 上划
				tmpXElement.addChild(new XElement("Abstract", "1025"));
			} else {
				tmpXElement.addChild(new XElement("Abstract", "1026"));
			}
			tmpXElement.addChild(new XElement("Postscript", p_req1908.singleDetail.abstractATS));

			if ("0".equals(trFlag)) { // 上划
				tmpXElement.addChild(new XElement("DbAccName", p_req1908.singleDetail.payName));
				tmpXElement.addChild(new XElement("CrAccName", p_req1908.singleDetail.recName));
			} else {
				tmpXElement.addChild(new XElement("DbAccName", p_req1908.singleDetail.recName));
				tmpXElement.addChild(new XElement("CrAccName", p_req1908.singleDetail.payName));
			}
            xmlWriter.writeXElement(tmpXElement);

			xmlWriter.writeStartElement("Cmp");
			if ("0".equals(trFlag)) { // 上划
				xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req1908.singleDetail.payAct));
				xmlWriter.writeElementString("DbProv", p_req1908.singleDetail.payArea);
				xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_req1908.singleDetail.payCur));
			} else {
				xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_req1908.singleDetail.recAct));
				xmlWriter.writeElementString("DbProv", p_req1908.singleDetail.recArea);
				xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_req1908.singleDetail.recCur));
			}
			xmlWriter.writeElementString("TrFlag", primaryAct);// 上划下拨标志 0-上划 1-下拨

			if ("0".equals(trFlag)) { // 上划
				xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_req1908.singleDetail.recAct));
				xmlWriter.writeElementString("CrProv", p_req1908.singleDetail.recArea);
				xmlWriter.writeElementString("CrCur", convertCurToBank(p_req1908.singleDetail.recCur));
			} else {
				xmlWriter.writeElementString("CrAccNo", getAbcAccNo(p_req1908.singleDetail.payAct));
				xmlWriter.writeElementString("CrProv", p_req1908.singleDetail.payArea);
				xmlWriter.writeElementString("CrCur", convertCurToBank(p_req1908.singleDetail.payCur));
			}
			xmlWriter.writeEndElement();

			xmlWriter.writeElementString("Amt", p_req1908.singleDetail.payAmount);
			xmlWriter.writeElementString("Fee", "");
			xmlWriter.writeElementString("AgtFee", "");
			nodeAnalysis(p_req1908.singleDetail.srcXElement, tmpXElement, getCharset());
			xmlWriter.writeXElement(tmpXElement);
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
					String tmpRespInfo = tmpBkReader.getFirstText("/ap/RespInfo");
					if (StringHelper.isNullOrEmpty(tmpRespInfo)) {
						tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
					}

					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpRespInfo;
					tmpRetDetail.transState = tmpRespCode;
					tmpRetDetail.payInfoCode = tmpRespCode;
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else if (ConnState.FAIL.equals(tmpStrRet[0])) {
				tmpRetDetail.respCode = "0";
				tmpRetDetail.respInfo = tmpStrRet[1];
				tmpRetDetail.transState = PaymentState.FAIL_Payment;
				tmpRetDetail.payInfoCode = tmpStrRet[0];
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return tmpRetDetail;
	}

	public RetAtsSglDetail d_190804(ReqSingle reqSingle) throws Exception {
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(reqSingle);
		
		try {
			ABC01Header tmpBankHeader = new ABC01Header();
			tmpBankHeader.transCode = ABC01TransCode.deal_1908_190804;
			tmpBankHeader.reqSeqID = reqSingle.singleDetail.askNumber;
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHead(xmlWriter, tmpBankHeader);
			
			XElement tmpXElement = new XElement("Corp", "");
			Date tmpDateNow = new Date();
			tmpXElement.addChild(new XElement("BookingDate", Format.Date8Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("BookingTime", Format.Time6Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("BookingFlag", "0"));
			tmpXElement.addChild(new XElement("Postscript", reqSingle.singleDetail.abstractATS));
			tmpXElement.addChild(new XElement("DbBankName", reqSingle.singleDetail.payBankName));
			tmpXElement.addChild(new XElement("CrAccName", reqSingle.singleDetail.recName));
			tmpXElement.addChild(new XElement("CrBankName", reqSingle.singleDetail.cnapsName));
			tmpXElement.addChild(new XElement("DbAccName", reqSingle.singleDetail.payName));
			tmpXElement.addChild(new XElement("VoucherNo", "0"));
			tmpXElement.addChild(new XElement("VoucherType", "0"));
			tmpXElement.addChild(new XElement("CshDraFlag", "0"));
			tmpXElement.addChild(new XElement("CrActCode", "0000"));
			
			xmlWriter.writeStartElement("Cmp");
			xmlWriter.writeElementString("DbAccNo", getAbcAccNo(reqSingle.singleDetail.payAct));
			xmlWriter.writeElementString("DbProv", reqSingle.singleDetail.payArea);
			xmlWriter.writeElementString("DbCur", convertCurToBank(reqSingle.singleDetail.payCur));
			xmlWriter.writeElementClosed("DbLogAccNo");
			
			xmlWriter.writeElementString("CrAccNo", getAbcAccNo(reqSingle.singleDetail.recAct));
			xmlWriter.writeElementString("CrProv", reqSingle.singleDetail.recArea);
			xmlWriter.writeElementString("CrCur", convertCurToBank(reqSingle.singleDetail.recCur));
			xmlWriter.writeElementString("ConFlag", "1");
			xmlWriter.writeEndElement();
			xmlWriter.writeElementString("Amt", reqSingle.singleDetail.payAmount);
			nodeAnalysis(reqSingle.singleDetail.srcXElement, tmpXElement, getCharset());
			xmlWriter.writeXElement(tmpXElement);
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
					String tmpRespInfo = tmpBkReader.getFirstText("/ap/RespInfo");
					if (StringHelper.isNullOrEmpty(tmpRespInfo)) {
						tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
					}
					tmpRetDetail.reqSeqID = reqSingle.singleDetail.askNumber;
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpRespInfo;
					tmpRetDetail.transState = tmpRespCode;
					tmpRetDetail.payInfoCode = tmpRespCode;
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch(Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return tmpRetDetail;
	}

	public RetAtsSglDetail d_190802(ReqSingle reqSingle) throws Exception {
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(reqSingle);

		try {
			ABC01Header tmpBankHeader = new ABC01Header();
			tmpBankHeader.transCode = ABC01TransCode.deal_1908_190802;
			tmpBankHeader.reqSeqID = reqSingle.singleDetail.askNumber;

			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHead(xmlWriter, tmpBankHeader);
			
			XElement tmpXElement = new XElement("Corp", "");
			Date tmpDateNow = new Date();
			tmpXElement.addChild(new XElement("BookingDate", Format.Date8Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("BookingTime", Format.Time6Format.format(tmpDateNow)));
			tmpXElement.addChild(new XElement("BookingFlag", "0"));
			tmpXElement.addChild(new XElement("ExchangeType", "0"));
			tmpXElement.addChild(new XElement("Postscript", reqSingle.singleDetail.abstractATS));
//			tmpXElement.addChild(new XElement("Postscript", reqSingle.singleDetail.purpose + "-" + reqSingle.singleDetail.abstractATS));
			tmpXElement.addChild(new XElement("ActInf", "0"));
			tmpXElement.addChild(new XElement("UrgencyFlag", reqSingle.singleDetail.paySpd));
			
			String sameBank = "";
			if ("1".equals(reqSingle.singleDetail.sameBnk)) {
				sameBank = "0";
			} else {
				sameBank = "1";
			}
			tmpXElement.addChild(new XElement("OthBankFlag", sameBank));
			tmpXElement.addChild(new XElement("DbBankName", reqSingle.singleDetail.payBankName));
			tmpXElement.addChild(new XElement("CrAccName", reqSingle.singleDetail.recName));
			tmpXElement.addChild(new XElement("CrBankName", reqSingle.singleDetail.cnapsName));
			tmpXElement.addChild(new XElement("CrBankNo", reqSingle.singleDetail.cnapsCode));
			tmpXElement.addChild(new XElement("DbAccName", reqSingle.singleDetail.payName));
			tmpXElement.addChild(new XElement("DbAddress", ""));
			tmpXElement.addChild(new XElement("CrAddress", ""));
			tmpXElement.addChild(new XElement("WhyUse", reqSingle.singleDetail.purpose));
			
			xmlWriter.writeStartElement("Cmp");
			xmlWriter.writeElementString("DbAccNo", getAbcAccNo(reqSingle.singleDetail.payAct));
			xmlWriter.writeElementString("DbProv", reqSingle.singleDetail.payArea);
			xmlWriter.writeElementString("DbCur", convertCurToBank(reqSingle.singleDetail.payCur));
			xmlWriter.writeElementClosed("DbLogAccNo");
			
			if("1".equals(reqSingle.singleDetail.sameBnk)) {
				xmlWriter.writeElementString("CrAccNo", getAbcAccNo(reqSingle.singleDetail.recAct));
				xmlWriter.writeElementString("CrProv", getAbcAccProv(reqSingle.singleDetail.recAct, reqSingle.singleDetail.recArea));
			} else {
				xmlWriter.writeElementString("CrAccNo", reqSingle.singleDetail.recAct);
				xmlWriter.writeElementString("CrProv", "");
			}
			
			xmlWriter.writeElementString("CrCur", convertCurToBank(reqSingle.singleDetail.recCur));
			xmlWriter.writeElementClosed("CrLogAccNo");
			xmlWriter.writeElementString("ConFlag", "1");
			xmlWriter.writeEndElement();
			xmlWriter.writeElementString("Amt", reqSingle.singleDetail.payAmount);
			nodeAnalysis(reqSingle.singleDetail.srcXElement, tmpXElement, getCharset());
			xmlWriter.writeXElement(tmpXElement);
			xmlWriter.writeEndDocument();

			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
			
			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
					String tmpRespInfo = tmpBkReader.getFirstText("/ap/RespInfo");
					if (StringHelper.isNullOrEmpty(tmpRespInfo)) {
						tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
					}
					tmpRetDetail.reqSeqID = reqHeader.reqSeqID;
					tmpRetDetail.respCode = "0";
					tmpRetDetail.respInfo = tmpRespInfo;
					tmpRetDetail.transState = tmpRespCode;
					tmpRetDetail.payInfoCode = tmpRespCode;
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			tmpRetDetail.respCode = "-1";
			tmpRetDetail.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return tmpRetDetail;
	}

	@Override
	public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
		RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
		RetAtsSglDetail tmpRetDetail = new RetAtsSglDetail(p_req1944);

		try {
			ABC01Header bankHeader = new ABC01Header();
			bankHeader.transCode = ABC01TransCode.deal_1944;
			bankHeader.reqSeqID = p_req1944.reqHeader.transSeqID;
			
			XmlTextWriter xmlWriter = new XmlTextWriter();
			writeCommonHead(xmlWriter, bankHeader);
			xmlWriter.writeStartElement("Cme");
			xmlWriter.writeElementString("SerialNo", p_req1944.askNumber);
			xmlWriter.writeEndDocument();
			
			String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

			if (tmpStrRet[0].length() == 0) {
				StringBuilder tmpSbMsg = new StringBuilder();
				XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
				if (tmpBkReader != null) {
					String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
					String tmpRespInfo = tmpBkReader.getFirstText("/ap/RespInfo");
					if (StringHelper.isNullOrEmpty(tmpRespInfo)) {
						tmpRespInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
					}
					String tmpRespData = tmpBkReader.getFirstText("/ap/Cmp/RespPrvData");
					if (StringHelper.hasAnyChar(tmpRespInfo)) {
						tmpRespInfo = tmpRespData;
					}
					
					if ("0000".equals(tmpRespCode)) {
						tmpRetDetail.respCode = "0";
						tmpRetDetail.respInfo = tmpRespInfo;
						tmpRetDetail.transState = tmpBkReader.getFirstText("/ap/Cmp/TransSta");
						tmpRetDetail.payInfoCode = tmpRespInfo;
					} else {
						tmpRetDetail.respCode = "-1";
						tmpRetDetail.respInfo = tmpRespInfo;
					}
				} else {
					tmpRetDetail.respCode = "-1";
					tmpRetDetail.respInfo = tmpSbMsg.toString();
				}
			} else {
				tmpRetDetail.respCode = "-1";
				tmpRetDetail.respInfo = tmpStrRet[1];
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.writeRespCode("-1");
			retAts.writeRespInfo(ex.getMessage());
			WriteBankLogLn(ex);
		}

		retAts.writeRetDetail(tmpRetDetail);
		return retAts;
	}

	@Override
	public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
		return null;
	}

	@Override
	public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
		return null;
	}
}